package tech.aistar.util;

import java.sql.*;

/**
 * 本类用来演示: jdbc工具类
 *
 * @author: success
 * @date: 2022/8/11 1:41 下午
 */
@SuppressWarnings("all")
public class JdbcUtil {

    private static final String URL = ConfigUtil.getValue("url");
    private static final String USERNAME = ConfigUtil.getValue("username");
    private static final String PASSWORD = ConfigUtil.getValue("password");

    public static Connection getConnection() throws SQLException {
        Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        return conn;
    }

    public static void close(Connection conn, Statement pst){
        if(null!=pst){
            try {
                pst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(null!=conn){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(Connection conn, Statement pst, ResultSet rs){
        if(null!=rs){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(null!=pst){
            try {
                pst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(null!=conn){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
